package com.tools;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.res.ResourcesCompat;

import com.example.login.R;

public class CommonDialog extends Dialog {
    private TextView titleTv ;
    private TextView messageTv ;
    private TextView negativeBtn, positiveBtn;
    private EditText editText;
    //按钮分割线
    private View columnLineView ;
    private Context mContext;
    public CommonDialog(Context context) {
        super(context, R.style.CustomDialog);
        mContext = context;
    }

    /**
     * 都是内容数据
     */
    private String message;
    private String title;
    private String positive, negative ;
    private int imageResId = -1 ;

    /**
     * 底部是否只有一个按钮
     */
    private boolean isSingle = false;

    /**
     * 保留EditText或者TextView
     */
    private boolean isEdit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_dialog);
        setCanceledOnTouchOutside(false);
        initView();
        refreshView();
        initEvent();
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        positiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( onClickBottomListener!= null) {
                    onClickBottomListener.onPositiveClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        negativeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( onClickBottomListener!= null) {
                    onClickBottomListener.onNegativeClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void refreshView() {
        //如果用户自定了title和message
        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);
        }else {
            titleTv.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(message)) {
            messageTv.setText(message);
        }
        //如果设置按钮的文字
        if (!TextUtils.isEmpty(positive)) {
            positiveBtn.setText(positive);
        }else {
            positiveBtn.setText("确定");
        }
        if (!TextUtils.isEmpty(negative)) {
            negativeBtn.setText(negative);
        }else {
            negativeBtn.setText("取消");
        }

        if (isSingle){
            columnLineView.setVisibility(View.GONE);
            negativeBtn.setVisibility(View.GONE);
        }else {
            negativeBtn.setVisibility(View.VISIBLE);
            columnLineView.setVisibility(View.VISIBLE);
        }

        if (isEdit){
            editText.setVisibility(View.VISIBLE);
            messageTv.setVisibility(View.GONE);
        }else {
            editText.setVisibility(View.GONE);
            messageTv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        negativeBtn = (TextView) findViewById(R.id.negative);
        positiveBtn = (TextView) findViewById(R.id.positive);
        titleTv = (TextView) findViewById(R.id.title);
        messageTv = (TextView) findViewById(R.id.message);

        editText = (EditText) findViewById(R.id.dialog_edittext);
        Drawable money = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.money, null);
        money.setBounds(0, 0, 50, 50);

        editText.setCompoundDrawables(money, null, null, null);
        columnLineView = findViewById(R.id.column_line);
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;
    public void setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
    }
    public interface OnClickBottomListener{
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick();
        /**
         * 点击取消按钮事件
         */
        public void onNegativeClick();
    }

    public String getMessage() {
        return message;
    }

    public CommonDialog setMessage(String message) {
        this.message = message;
        return this ;
    }

    public String getEdit(){
        return editText.getText().toString();
    }

    public String getTitle() {
        return title;
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this ;
    }

    public String getPositive() {
        return positive;
    }

    public CommonDialog setPositive(String positive) {
        this.positive = positive;
        return this ;
    }

    public String getNegative() {
        return negative;
    }

    public CommonDialog setNegative(String negative) {
        this.negative = negative;
        return this ;
    }

    public int getImageResId() {
        return imageResId;
    }

    public boolean isSingle() {
        return isSingle;
    }

    public CommonDialog setSingle(boolean single) {
        isSingle = single;
        return this ;
    }

    public CommonDialog retainEdit(boolean edit){
        isEdit = edit;
        return this;
    }

    public CommonDialog setImageResId(int imageResId) {
        this.imageResId = imageResId;
        return this ;
    }
}


